import { createRouter, createWebHistory } from 'vue-router'
import VueCookies from 'vue-cookies'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import("@/views/Login.vue")
    },
    {
      path: '/',
      name: 'layout',
      component: () => import('@/views/Layout.vue'),
      children:
        [
          {
            path: '/home',
            name: '首页',
            component: () => import('@/views/home/Home.vue')
          }, 
          {
            path: '/business/classification',
            name: '菜品分类',
            component: () => import('@/views/business/Classification.vue')
          }, 
          {
            path: '/business/product',
            name: '菜品管理',
            component: () => import('@/views/business/Product.vue')
          }, 
          {
            path: '/system/merchant',
            name: '商家管理',
            component: () => import('@/views/system/Merchant.vue')
          }, 
          {
            path: '/review/merchant',
            name: '商家审核',
            component: () => import('@/views/review/Merchant.vue')
          }, 
          {
            path: '/review/commodity',
            name: '菜品审核',
            component: () => import('@/views/review/Commodity.vue')
          }, 
          {
            path: '/profile/mine',
            name: '个人中心',
            component: () => import('@/views/profile/Mine.vue')
          }
        ]
    }
  ],
})

router.beforeEach((to, from, next) => {
  const userInfo = VueCookies.get("userInfo")
  if (userInfo == null && to.path != "/login") {
    router.push("/login")
  }
  next();
})

export default router
